package net.cassite.daf4j.ds;

import net.cassite.daf4j.*;

/**
 * 条件处理.解析Condition对象并调用ConditionParser对应方法
 *
 * @see net.cassite.daf4j.Condition
 * @see ConditionParser
 */
public class ConditionResolver<Context, BasicElement> {
        private final ConditionParser<Context, BasicElement> conditionParser;

        public ConditionResolver(ConditionParser<Context, BasicElement> conditionParser) {
                this.conditionParser = conditionParser;
        }

        public BasicElement resolve(Context context, Condition condition) throws Exception {
                if (condition.type == ConditionTypes.between) {
                        return conditionParser.between(context, (ParameterComparable<?>) condition.data, condition.args.get(0), condition.args.get(1));
                } else if (condition.type == ConditionTypes.eq) {
                        return conditionParser.eq(context, condition.data, condition.args.get(0));
                } else if (condition.type == ConditionTypes.ge) {
                        return conditionParser.ge(context, (ParameterComparable<?>) condition.data, condition.args.get(0));
                } else if (condition.type == ConditionTypes.gt) {
                        return conditionParser.gt(context, (ParameterComparable<?>) condition.data, condition.args.get(0));
                } else if (condition.type == ConditionTypes.in) {
                        return conditionParser.in(context, condition.data, (PreResult<?>) condition.args.get(0));
                } else if (condition.type == ConditionTypes.isNotNull) {
                        return conditionParser.isNotNull(context, condition.data);
                } else if (condition.type == ConditionTypes.isNull) {
                        return conditionParser.isNull(context, condition.data);
                } else if (condition.type == ConditionTypes.le) {
                        return conditionParser.le(context, (ParameterComparable<?>) condition.data, condition.args.get(0));
                } else if (condition.type == ConditionTypes.like) {
                        return conditionParser.like(context, condition.data, (Object[]) condition.args.get(0));
                } else if (condition.type == ConditionTypes.lt) {
                        return conditionParser.lt(context, (ParameterComparable<?>) condition.data, condition.args.get(0));
                } else if (condition.type == ConditionTypes.member) {
                        return conditionParser.member(context, condition.data, (ParameterAggregate) condition.args.get(0));
                } else if (condition.type == ConditionTypes.ne) {
                        return conditionParser.ne(context, condition.data, condition.args.get(0));
                } else if (condition.type == ConditionTypes.notIn) {
                        return conditionParser.notIn(context, condition.data, (PreResult<?>) condition.args.get(0));
                } else if (condition.type == ConditionTypes.notMember) {
                        return conditionParser.notMember(context, condition.data, (ParameterAggregate) condition.args.get(0));
                } else if (condition.type == ConditionTypes.reverseMember) {
                        return conditionParser.reverseMember(context, (ParameterAggregate) condition.data, condition.args.get(0));
                } else if (condition.type == ConditionTypes.reverseNotMember) {
                        return conditionParser.reverseNotMember(context, (ParameterAggregate) condition.data, condition.args.get(0));
                } else {
                        throw new IllegalArgumentException();
                }
        }
}
